<?php


namespace App\Common\Business;
use App\Common\Model\MrpChangeLog as MrpChangeLogModel;
use Illuminate\Support\Facades\Log;

class MrpChangeLog
{
    public $model;
    public function __construct()
    {
        $this->model = new MrpChangeLogModel();
    }

    public function recordMrpChangeTopLastWeek($top = 10)
    {
        // 上周开始和结束时间
        $beginLastweek = mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
        $endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));

        $res = $this->model->selectRaw('sum(change_num) as num, uid')
            ->whereRaw('created_at > '.$beginLastweek.' and created_at < '.$endLastweek.' and change_type = 0')
            ->groupBy('uid')
            ->orderBy('num', 'desc')
            ->limit($top)
            ->get()->toArray();

        if (!$res) {
            return false;
        }
        return $res;
    }
}
